<template>

  <div class="app-container">

       <h2 style="text-align: center;">发布新文章</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写文章基本信息"/>
      <el-step title="编写文章内容"/>
      <el-step title="最终发布"/>
    </el-steps>
    <el-form label-width="120px">


     <el-form-item label="文章内容">
            <tinymce :height="300" v-model="blogContent.content" />
    </el-form-item>
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
import content from '@/api/tingting/content'
export default {
    components:{Tinymce},
    data() {
        return {
            blogContent:{
                id:'',
                blogId:'',
                content:''
            }, 
            saveBtnDisabled:false
        }
    },
    created() {
        if(this.$route.params && this.$route.params.id) {
            this.blogContent.blogId = this.$route.params.id
            this.findContentbyBlogId()
           
        }
            
    },
    methods:{
        previous() {
            this.$router.push({path:'/blog/info/'+this.blogContent.blogId})
        },
        addContent() {
            content.addContent(this.blogContent)
            .then(response => {
              this.$message({
               type: 'success',
               message: '添加成功!'
               });
                 this.$router.push({path:'/blog/publish/'+this.blogContent.blogId})
            })
          
        },
        updateContent(){
          content.updateContent(this.blogContent)
            .then(response => {
               this.$message({
               type: 'success',
               message: '修改成功!'
               });
               this.$router.push({path:'/blog/publish/'+this.blogContent.blogId})
            })
        },
        saveOrUpdate(){
           if(this.blogContent.id===''){
                this.addContent()
            }else{
                this.updateContent()
            }
        },
        findContentbyBlogId(){
          content.findContentbyBlogId(this.blogContent.blogId)
            .then(response => {
            if( response.data.blogcontent.length!=0)
             this.blogContent = response.data.blogcontent[0]
            })
        }
    }
}
</script>
<style scoped>
.tinymce-container{
    line-height: 29px;
}
</style>